﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>
<div id="chatLog"></div>
<script type="text/javascript">
    $(function () {
        if (!("WebSocket" in window)) {
            
        } else {
            connect();
            
            function connect() {
                var socket;
                var host = "ws://http://localhost:44937/socket/demo";
                try {
                    socket = new WebSocket(host);
                    message("<p class='event'>Socket Status:" + socket.readyState);

                    socket.onopen = function() {
                        message("<p class='event'>Socket Status:" + socket.readyState + "(open)");
                    };

                    socket.onmessage = function(msg) {
                        message("<p class='event'>Received:" + msg.data);
                    };

                    socket.onclose = function() {
                        message("<p class='event'>Socket Status:" + socket.readyState + "(closed)");
                    };
                }
                catch (exception) {
                    message("<p>Error:" + exception);
                }
                
                function message(msg) {
                    $("#chatLog").append(msg + "</p>");
                }
            }
        }
    })
</script>